<extend name="Layout/ins_page" />
<block name="content">
    <link href="/sap/demo.css" rel="stylesheet" type="text/css" />
    <script src="/sap/scripts/boot.js" type="text/javascript"></script>
    <style>
        table img{width: 40px}
        .myrow
        {
            background:#fceee2;
        }

        .mini-grid-newRow {
            background: white;
        }
    </style>
    <body style="">

    选择供应商:<input  label="ComboBox："   id="gys" class="mini-buttonedit" onbuttonclick="onButtonEdit2"   value="<?php echo $info['suppliers_code']?>" text="<?php echo $info['suppliers_name']?>" name="" textName="" style="width: 200px"/>
    采购类型: <input  id='cgtype'   name="" showNullItem="true" class="mini-combobox" data="[{'id':1,'text':'集采'},{'id':2,'text':'非常规'},{'id':3,'text':'常规'}]" value="<?php echo $info['type']?>" valueField="id" textField="text"  onValuechanged='cgfschange' />
    产品负责人: <input  id='cplist'  name="" showNullItem="true" class="mini-combobox" url="/stocking/getUser?type=1" valueField="id" textField="text"   value="<?php echo $info['manager']?>" /> <br/> <br/>
    营销负责人: <input id='salelist' name="" showNullItem="true" class="mini-combobox" url="/stocking/getUser?type=2" valueField="id" textField="text"  value="<?php echo $info['sale']?>" style="width: 200px"/>
    到货时间:<input property="" class="mini-datepicker" valueType='string' style="width:200px;" format="yyyy-MM-dd" id='storage_time' value="<?php echo $info['storage_time']?>"  />
    采购员: <input id='receipter' name="" showNullItem="true" class="mini-combobox" url="/stocking/getreceipter" valueField="slpcode" textField="SlpName"  value="<?php echo $info['receipterCode']?>" style="width: 200px"/>
    <br/>
    <div id="tabs1" class="mini-tabs" activeIndex="0" style="width:100%;margin-top: 5px;overflow:auto;" plain="false"
         buttons="#tabsButtons"
    >
        <div title="备货明细" >

            <div id="datagrid1" class="mini-datagrid" style="height:400px;width: 100%;"
                 allowCellEdit="true" allowCellSelect="true" multiSelect="false" showPager="false"
                 allowCellValid="true" oncellvalidation="onCellValidation" ondrawcell="ondrawcell">

                <div property="columns">


                    <div field="action"  headerAlign="center" onclick="deleterow(this)"  >

                    </div>


                    <div field="item_code"  headerAlign="center"  >物料号
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="item_name"  headerAlign="center" width='300px' >物料名称
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="p_price"  headerAlign="center"  >本次采购价格
                        <input property="editor" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="previous_price"  headerAlign="center"  >上次采购价格
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="warehouse"  headerAlign="center"   type="comboboxcolumn" autoShowPopup="true" >仓库
                        <input property="editor" class="mini-combobox" style="width:100%;" data="whouselist" />
                    </div>

                    <div field="change_price"  headerAlign="center"  >价格变动率(%)
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>


                    <div field="amout"  headerAlign="center"  >申请备货数量
                        <input property="editor" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="total_price"  headerAlign="center"  >备货金额
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="r_inventory"  headerAlign="center"  >安全库存
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="current_inventory"  headerAlign="center"  >在库库存
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="transit_inventory"  headerAlign="center"  >在途库存
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="available_inventory"  headerAlign="center"  >可用库存
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <!--  <div field="storage_time"   headerAlign="center"  >到货日期
                          <input property="editor" class="mini-datepicker" valueType='string' style="width:100%;" format="yyyy-MM-dd H:mm:ss" timeFormat="H:mm:ss" showTime="true" />
                      </div>
                       -->

                    <div field="lately_7"  headerAlign="center"  >近7天销量
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="lately_15"  headerAlign="center"  >近15天销量
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>



                    <div field="lately_30"  headerAlign="center"  >前30天销量
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="taotal_sales"  headerAlign="center"  >累计销量
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="suggestorder"  headerAlign="center"  >建议备货数量
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="availableday"  headerAlign="center"  >可用库存天数
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="avgbuyday"  headerAlign="center"  >平均订货天数
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="avgdaliysale"  headerAlign="center"  >平均日销量
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="minsafe"  headerAlign="center"  >最低安全库存
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>

                    <div field="remarkes"  headerAlign="center"  >备注
                        <input property="" class="mini-textbox" style="width:100%;" />
                    </div>


                </div>
            </div>

        </div>
        <!-- <div title="Tab2" iconCls="" >
            2
        </div> -->
    </div>

    <div id='editForm1'>
        <form id="beizhuForm">
            <br/> <br/>
            备注: <input required="true" id="remarkes" class="mini-textarea" style="width:400px;" minWidth="200" minHeight="100" value="<?php echo $info['remarkes']?>" />
            <br/> <br/>
            <a class="mini-button" onclick="onButtonEdit()" enabled="" id='query-bt' >&nbsp;&nbsp;添加物料&nbsp;&nbsp;</a>
            <a class="mini-button" onclick="save()" enabled="" id='query-bt' >&nbsp;&nbsp;保存&nbsp;&nbsp;</a>
        </form>
    </div>

    </body>
    <script type="text/javascript">
        mini.parse();
        var grid = mini.get("datagrid1");
        <?php if( $data) {?>
            var data = <?php echo $data;?>;
            grid.setData(data);
        <?php }?>

        var whouselist = [{ id: 'HYWCK', text: '联谊路仓库' }, { id: 'A07', text: '样品仓库'},{ id: 'A02', text: '虚拟卡券'},{ id: 'H03', text: '3号仓库'}];
        function onCellValidation(e){
            var record = e.record,
                column = e.column,
                field = e.field,
                value = e.value;
            var row = grid.getSelected();

            if (field == "p_price") {
                if(record.previous_price>0)
                    record.change_price = toDecimal(((toDecimal(value)-toDecimal(record.previous_price))/record.previous_price)*100);
                else
                    record.change_price = 0;
            }

            if (field == "amout"||field == "p_price") {
                record.total_price=toDecimal(value)*toDecimal(record.p_price);
            }

            if (field == "p_price") {
                record.total_price=toDecimal(value)*toDecimal(record.amout);
            }



            grid.updateRow ( row, record );
        }




        grid.on("drawcell", function (e) {
            var record = e.record,
                column = e.column,
                field = e.field,
                value = e.value;

            //格式化日期
            if (field == "storage_time"||field=='storage_time') {
                if (mini.isDate(value)) e.cellHtml = mini.formatDate(value, "yyyy-MM-dd HH:mm:ss");

            }


            if (field == "amout"||field == "total_price"||field=="storage_time"||field=="p_price") {
                e.cellStyle = "background:#ecedef";
            }


            if(field=='action'){
                e.cellHtml = '<a href="javascript:void(0)" onclick="deleterow()">删除</a>&nbsp; ';

            }

            if(field=='warehouse'){

            }


        });


        function deleterow(e){

            var row = grid.getSelected ();
            var item = row.item_name;
            mini.confirm("确定删除 "+item+"?", "确定？",
                function (action) {
                    if (action == "ok") {
                        grid.removeRow(grid.getSelected ());
                    } else {
                    }
                }
            );


        }


        function onButtonEdit2(e) {
            var btnEdit = this;
            mini.open({
                url:  "/index.php?m=&c=Stocking&a=supplierselect",
                title: "选择列表",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        if (data) {
                            btnEdit.setValue(data.cardcode);
                            btnEdit.setText(data.cardName);
                        }
                    }

                }
            });

        }


        function onButtonEdit(e) {
            var gys_code =  mini.get('gys').getValue();
            if(gys_code==null||gys_code==''){
                mini.alert("请先选择供应商");
                return ;
            }
            var btnEdit = this;
            mini.open({
                url:  "/sap/goods_select_simple.php",
                title: "选择物料",
                width: 650,
                height: 380,
                ondestroy: function (action) {
                    //if (action == "close") return false;
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        if (data) {
                            //data.Code=mini.get('U_CardCode').getValue();
                            data.item_code=data.catid;
                            data.item_name=data.name;
                            var info = getiteminfo(data.item_code);
                            data.r_inventory = info.r_inventory;
                            data.lately_7   = info.lately_7;
                            data.lately_15   = info.lately_15;
                            data.lately_30   = info.lately_30;
                            data.taotal_sales= info.taotal_sales;

                            data.suggestorder = info.suggestorder;
                            data.availableday = info.availableday;
                            data.avgbuyday = info.avgbuyday;
                            data.avgdaliysale = info.avgdaliysale;
                            data.minsafe = info.minsafe;

                            data.previous_price     = toDecimal(info.previous_price);
                            data.current_inventory  = info.current_inventory;
                            data.transit_inventory  = info.transit_inventory;
                            data.available_inventory= info.available_inventory;
                            data.suppliers_code = mini.get('gys').getValue();
                            data.suppliers_name = mini.get('gys').getText();
                            data.warehouse = info.warehouseCode;
                            grid.addRow(data, 0);

                        }
                    }

                }
            });
        }

        function getiteminfo(itemcode){
            var returndata = null;
            $.ajax({
                url: "/Stocking/getItemInfo",
                data: { itemCode: itemcode},
                type: "post",
                dataType:'json',
                async: false,
                success: function (data) {
                    returndata = data;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
            return returndata;
        }

        function save() {
            //提交表单数据
            var form = new mini.Form("#beizhuForm");
            form.validate();
            if (form.isValid() == false) {
                mini.unmask(document.body);
                mini.alert('请完成必填项!');
                return;
            }

            var type = mini.get("cgtype").getValue();
            var cp = mini.get("cplist").getValue();
            var sale = mini.get("salelist").getValue();
            var storage_time = mini.get('storage_time').getValue();
            var receipter = mini.get('receipter').getText();
            var receipterCode = mini.get('receipter').getValue();
            if(receipter==null||receipter==''){
                mini.alert("请选择采购员");
                return ;
            }
            if(type==null||type==''){
                mini.alert("请选择采购方式");
                return ;
            }
            if(storage_time==null||storage_time==''){
                mini.alert("请输入到货时间");
                return ;
            }
            if(cp==null||cp==''){
                mini.alert('必须选择产品负责人');
                return ;
            }

            if(type!=3){
                if(sale==null||sale==''){
                    mini.alert("采集与非常规方式必须选择营销负责人");
                    return ;
                }
            }
            else{

                if(sale!=''){
                    mini.alert("常规方式无需选择营销负责人");
                    return ;
                }
            }

            var gys_code =  mini.get('gys').getValue();
            var gys_name =  mini.get('gys').getText();
            var remarkes =  mini.get('remarkes').getValue();
            var data = grid.getData();
            if(data==null){
                mini.alert('无更新数据');
                return;
            }
            for(var i =0;i<data.length;i++){


                if(data[i].item_code==null||data[i].item_code==''){
                    mini.alert(data[i].item_name +' 物料不能为空');
                    return ;
                }

                if(data[i].suppliers_code==null||data[i].suppliers_code==''){
                    mini.alert(data[i].item_name+' 供应商不能为空');
                    return ;
                }

                if(data[i].amout==null||data[i].amout<=0){
                    mini.alert(data[i].item_name+' 备货数量不能为空');
                    return ;
                }


            }

            if(data.length==0){
                mini.alert('请最少添加一个物料');
                return ;
            }




            var json = mini.encode(data);
            mini.mask({
                el: document.body,
                cls: 'mini-mask-loading',
                html: '保存中...'
            });

            $.ajax({
                url: "/Sale/doSalesOrderAdd",
                data: { data: json,receipterCode:receipterCode,type:type,cp:cp,sale:sale,gys_code:gys_code,gys_name:gys_name,remarkes:remarkes,storage_time:storage_time,receipter:receipter},
                type: "post",
                dataType:'json',
                success: function (data) {
                    if(data.code==0){
                        mini.unmask(document.body);
                        mini.alert('保存成功!');
                        search();
                        return ;
                    }else{
                        mini.unmask(document.body);
                        mini.alert(data.msg);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
        }

        //采购方式发生改变
        function cgfschange(e){
            var obj = this;
            if(this.getValue()==3){
                mini.get('salelist').disable(true);
                mini.get('salelist').setValue(null);
            }else{
                mini.get('salelist').enable(true);
            }

        }

        function onValueChanged(e) {

            var item = e.selected;
            if (item) {
                mini.get('cname-show').setValue(item.text);
            }
        }



        function toDecimal(x) {
            var f = parseFloat(x);
            if (isNaN(f)) {
                return 0;
            }
            f = Math.round(x*100)/100;
            return f;
        }

    </script>

    <script type="text/javascript">

        function ondrawcell(e) {
            var row = e.row,
                column = e.column,
                field = e.field,
                value = e.value;
            //超链接任务
            if (field == "item_code") {
                e.cellHtml = '<a id="' + row.item_code + '" class="showItem" data-placement="bottomleft">' + row.item_code + '</a>';
            }
        }

        var box;
        jQuery('.showItem').live('click',function(){
            mini.hideMessageBox(box);
            //loading();
            var id = jQuery(this).html();
            showDetailsOfItem(id);
//            $.ajax({
//                url: "/sap/getware.php",
//                data: { id: id },
//                async: true,
//                success: function (text) {
//                    mini.unmask(document.body);
//                    htmlClick(text);
//
//                }
//            });

        })
        function loading() {
            mini.mask({
                el: document.body,
                cls: 'mini-mask-loading',
                html: '加载库存信息..'
            });

        }
        function htmlClick(htmlContent) {
            box=  mini.showMessageBox({
                width: 550,
                title: "库存情况",
                html: htmlContent,
                showModal: false,
            });
        }

        function showDetailsOfItem(e) {

            var btnEdit1 = e.sender;
            var row = grid.getSelected();
            var json = mini.encode(row);

            mini.open({
                url : "/sap/item_detail.php?id=" + e,
                title : "物料相关",
                width : 650,
                height : 280,
                onload : function() {
                },
                ondestroy : function(action) {
                    if (action == "ok") {
                        CloseWindow();
                        mini.alert('成功....');
                        grid.reload();
                    }
                }
            });
        }

        function CloseWindow(action) {
            if (window.CloseOwnerWindow)
                return window.CloseOwnerWindow(action);
            else
                window.close();
        }

    </script>

    </body>
</block>


<block name="js">
</block>